package com.tang.lts.core.autoconfigure.resolver;

import com.tang.lts.core.autoconfigure.AutoConfigContext;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * @author Tang
 * @classname AbstractResolver
 * @description [  ]
 * @date 2022/4/13 20:56
 */
public abstract class AbstractResolver implements Resolver {

    protected void doFilter(AutoConfigContext autoConfigContext,PropertyDescriptor descriptor,String includeName,Filter filter) {
        String value = autoConfigContext.getPropsMap().get(includeName);
        // 类的字段 合格的key  value
        if (filter.onCondition(descriptor.getName(),includeName, value)) {
            filter.call(descriptor.getName(),includeName, value);
        }
    }

    protected void writeProperty(AutoConfigContext autoConfigContext, PropertyDescriptor propertyDescriptor, Object value) {
        Method writeMethod = propertyDescriptor.getWriteMethod();
        try {
            writeMethod.invoke(autoConfigContext.getTargetObject(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected interface Filter {

        /**
         * 匹配合适的key
         * @param name
         * @param key
         * @param value
         * @return
         */
        boolean onCondition(String name, String key, String value);

        /**
         *
         * @param name
         * @param key
         * @param value
         * @return
         */
        boolean call(String name,String key,String value);
    }

}